package com.ruoyi.campadmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.campadmin.mapper.CaItemRentalMapper;
import com.ruoyi.campadmin.service.ICaItemRentalService;
import com.ruoyi.campadmin.domain.CaItemRental;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.campadmin.domain.CaCamp;
import com.ruoyi.campadmin.service.ICaCampService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CaItemRentalServiceImpl implements ICaItemRentalService {


    @Resource
    private CaItemRentalMapper caItemRentalMapper;
    
    
    @Resource
    private ICaCampService caCampService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param caItemRental 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<CaItemRental> getCaItemRentalPage(PageDomain pageDomain, CaItemRental caItemRental , CaCamp caCamp) {
        if (Objects.isNull(caItemRental)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<CaItemRental> queryWrapper = new LambdaQueryWrapper<>(new CaItemRental());
            //if(!Objects.isNull(caItemRental.getName())){
            //	queryWrapper.like(CaItemRental::getName,caItemRental.getName());
            //}
            

            if (!Objects.isNull(caItemRental.getId())) {
                queryWrapper.eq(CaItemRental::getId, caItemRental.getId());
            }

            if (!Objects.isNull(caItemRental.getCaCampId())) {
                queryWrapper.eq(CaItemRental::getCaCampId, caItemRental.getCaCampId());
            }

            caCamp.setBaseEntityNull();
            caCamp.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(caCamp)) {
                List<Long> idList = caCampService.getIdListByCaCamp(caCamp);
                queryWrapper.in(CaItemRental::getCaCampId, idList);
            }


            if (!Objects.isNull(caItemRental.getTitleText())) {
                queryWrapper.like(CaItemRental::getTitleText, caItemRental.getTitleText());
            }

            if (!Objects.isNull(caItemRental.getDetailText())) {
                queryWrapper.like(CaItemRental::getDetailText, caItemRental.getDetailText());
            }

            if (!Objects.isNull(caItemRental.getPhotoUrl())) {
                queryWrapper.like(CaItemRental::getPhotoUrl, caItemRental.getPhotoUrl());
            }

            if (!Objects.isNull(caItemRental.getPrice())) {
                queryWrapper.like(CaItemRental::getPrice, caItemRental.getPrice());
            }

            if (!Objects.isNull(caItemRental.getPercentage())) {
                queryWrapper.like(CaItemRental::getPercentage, caItemRental.getPercentage());
            }

            if (!Objects.isNull(caItemRental.getService())) {
                queryWrapper.like(CaItemRental::getService, caItemRental.getService());
            }

            if (!Objects.isNull(caItemRental.getInventory())) {
                queryWrapper.like(CaItemRental::getInventory, caItemRental.getInventory());
            }

            if (!Objects.isNull(caItemRental.getPraiseNum())) {
                queryWrapper.like(CaItemRental::getPraiseNum, caItemRental.getPraiseNum());
            }

            if (!Objects.isNull(caItemRental.getCollectionNum())) {
                queryWrapper.like(CaItemRental::getCollectionNum, caItemRental.getCollectionNum());
            }

            if (!Objects.isNull(caItemRental.getStatus())) {
                queryWrapper.like(CaItemRental::getStatus, caItemRental.getStatus());
            }

            if (!Objects.isNull(caItemRental.getDelFlag())) {
                queryWrapper.like(CaItemRental::getDelFlag, caItemRental.getDelFlag());
            }

            if (!Objects.isNull(caItemRental.getCreateBy())) {
                queryWrapper.like(CaItemRental::getCreateBy, caItemRental.getCreateBy());
            }

            if (!Objects.isNull(caItemRental.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caItemRental.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caItemRental.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaItemRental::getCreateTime, caItemRental.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caItemRental.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaItemRental::getCreateTime, caItemRental.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caItemRental.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caItemRental.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaItemRental::getCreateTime, caItemRental.getParams().get("CreateTimeBeginTime"), caItemRental.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caItemRental.getUpdateBy())) {
                queryWrapper.like(CaItemRental::getUpdateBy, caItemRental.getUpdateBy());
            }

            if (!Objects.isNull(caItemRental.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caItemRental.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caItemRental.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaItemRental::getUpdateTime, caItemRental.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caItemRental.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaItemRental::getUpdateTime, caItemRental.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caItemRental.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caItemRental.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaItemRental::getUpdateTime, caItemRental.getParams().get("UpdateTimeBeginTime"), caItemRental.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caItemRental.getRemark())) {
                queryWrapper.like(CaItemRental::getRemark, caItemRental.getRemark());
            }
        Page<CaItemRental> caItemRentalList= caItemRentalMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(CaItemRental record:caItemRentalList.getRecords()){
            record.setCaCamp(caCampService.getCaCampById(record.getCaCampId()));
        }

            return caItemRentalList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public CaItemRental getCaItemRentalById(Long id) {
        if (Objects.isNull(id)) {
            return new CaItemRental();
        }
        try {
            CaItemRental caItemRental = caItemRentalMapper.selectById(id);
            caItemRental.setCaCamp(caCampService.getCaCampById(caItemRental.getCaCampId()));
            if (Objects.isNull(caItemRental)){
                caItemRental = new CaItemRental();
            }
            return caItemRental;

        } catch (Exception e) {
            e.printStackTrace();
            return new CaItemRental();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param caItemRental 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByCaItemRental(CaItemRental caItemRental) {
        if (Objects.isNull(caItemRental)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<CaItemRental> queryWrapper = new LambdaQueryWrapper<>(new CaItemRental());
            

            if (!Objects.isNull(caItemRental.getId())) {
                queryWrapper.eq(CaItemRental::getId, caItemRental.getId());
            }

            if (!Objects.isNull(caItemRental.getCaCampId())) {
                queryWrapper.eq(CaItemRental::getCaCampId, caItemRental.getCaCampId());
            }

            if (!Objects.isNull(caItemRental.getTitleText())) {
                queryWrapper.like(CaItemRental::getTitleText, caItemRental.getTitleText());
            }

            if (!Objects.isNull(caItemRental.getDetailText())) {
                queryWrapper.like(CaItemRental::getDetailText, caItemRental.getDetailText());
            }

            if (!Objects.isNull(caItemRental.getPhotoUrl())) {
                queryWrapper.like(CaItemRental::getPhotoUrl, caItemRental.getPhotoUrl());
            }

            if (!Objects.isNull(caItemRental.getPrice())) {
                queryWrapper.like(CaItemRental::getPrice, caItemRental.getPrice());
            }

            if (!Objects.isNull(caItemRental.getPercentage())) {
                queryWrapper.like(CaItemRental::getPercentage, caItemRental.getPercentage());
            }

            if (!Objects.isNull(caItemRental.getService())) {
                queryWrapper.like(CaItemRental::getService, caItemRental.getService());
            }

            if (!Objects.isNull(caItemRental.getInventory())) {
                queryWrapper.like(CaItemRental::getInventory, caItemRental.getInventory());
            }

            if (!Objects.isNull(caItemRental.getPraiseNum())) {
                queryWrapper.like(CaItemRental::getPraiseNum, caItemRental.getPraiseNum());
            }

            if (!Objects.isNull(caItemRental.getCollectionNum())) {
                queryWrapper.like(CaItemRental::getCollectionNum, caItemRental.getCollectionNum());
            }

            if (!Objects.isNull(caItemRental.getStatus())) {
                queryWrapper.like(CaItemRental::getStatus, caItemRental.getStatus());
            }

            if (!Objects.isNull(caItemRental.getDelFlag())) {
                queryWrapper.like(CaItemRental::getDelFlag, caItemRental.getDelFlag());
            }

            if (!Objects.isNull(caItemRental.getCreateBy())) {
                queryWrapper.like(CaItemRental::getCreateBy, caItemRental.getCreateBy());
            }

            if (!Objects.isNull(caItemRental.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caItemRental.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caItemRental.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaItemRental::getCreateTime, caItemRental.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caItemRental.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaItemRental::getCreateTime, caItemRental.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caItemRental.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caItemRental.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaItemRental::getCreateTime, caItemRental.getParams().get("CreateTimeBeginTime"), caItemRental.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caItemRental.getUpdateBy())) {
                queryWrapper.like(CaItemRental::getUpdateBy, caItemRental.getUpdateBy());
            }

            if (!Objects.isNull(caItemRental.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caItemRental.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caItemRental.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaItemRental::getUpdateTime, caItemRental.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caItemRental.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaItemRental::getUpdateTime, caItemRental.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caItemRental.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caItemRental.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaItemRental::getUpdateTime, caItemRental.getParams().get("UpdateTimeBeginTime"), caItemRental.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caItemRental.getRemark())) {
                queryWrapper.like(CaItemRental::getRemark, caItemRental.getRemark());
            }

            List<CaItemRental> caItemRentals = caItemRentalMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CaItemRental caItemRentalNo:caItemRentals){
                idList.add(caItemRentalNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过caCamp外键idList查询idList
     *
     * @param caCampIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByCaCampIdList(List<Long> caCampIdList) {
        if (Objects.isNull(caCampIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<CaItemRental> queryWrapper = new LambdaQueryWrapper<>(new CaItemRental());
            queryWrapper.in(CaItemRental::getCaCampId, caCampIdList);
            List<CaItemRental> caItemRentals = caItemRentalMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CaItemRental caItemRental:caItemRentals){
                idList.add(caItemRental.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param caItemRental 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertCaItemRental(CaItemRental caItemRental) {
        if (Objects.isNull(caItemRental)) {
            return -1;
        }
        try {
        
            //caItemRental.setSpell(PinYin.getPinYin(caItemRental.getName()));
            return caItemRentalMapper.insert(caItemRental);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param caItemRental 实体类
     * @return 结果
     */
    @Override
    public int updateCaItemRentalById(CaItemRental caItemRental) {
        if (Objects.isNull(caItemRental)) {
            return -1;
        }
        try {
            //caItemRental.setSpell(PinYin.getPinYin(caItemRental.getName()));
            return caItemRentalMapper.updateById(caItemRental);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteCaItemRentalByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return caItemRentalMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<CaItemRental> getCaItemRentalList() {
        try {
            LambdaQueryWrapper<CaItemRental> queryWrapper = new LambdaQueryWrapper<>(new CaItemRental());
            queryWrapper.isNotNull(CaItemRental::getId);
            return caItemRentalMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
